import ao
import sys
import ogg.vorbis
import threading

class OGG(threading.Thread):
    def __init__(self, songtitle, device_type="oss"):
        threading.Thread.__init__(self)
        self.mf = ogg.vorbis.VorbisFile(songtitle)
        self.info = self.mf.info()
        self.dev = ao.AudioDevice(device_type, rate=self.info.rate)
        self.play = True
        self.paused = False

    def run(self):
        while self.play:
            if self.paused == False:
                (buf, bytes, bit) = self.mf.read(4096)
                if bytes is 0:
                    self.mf.time_seek(0)
                self.dev.play(buf, bytes)
                
    def pauseit(self):
        if self.paused is True:
            self.paused = False
            return
        if self.paused is False:
            self.mf.time_seek(0)
            self.paused = True
            return
            
    def stop(self):
        self.play = False